package com.powerunion.datacollection.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.FormSetDao;
import com.powerunion.datacollection.model.FieldItem;
import com.powerunion.datacollection.model.FormSet;
import com.powerunion.datacollection.model.SysServer;
import com.powerunion.util.log.LogHelper;

public class FormSetManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			FormSetManager.class);
	private FormSetDao dao = null;

	public FormSetManager() {
		super();
		this.dao = (FormSetDao) DaoFactory.getInstance().getDao(
				FormSetDao.class);
	}

	public boolean save(String saveStr)throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		FormSet formSet = (FormSet) JSONObject.toBean(
				jsonObject, FormSet.class);
		
		return this.dao.save(formSet);
	}
	
	public boolean delete(String id) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.delete(id);
	}
	
	
	public FormSet findFormSetByFormId(String formId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.findFormSetByKey(formId);
	}
	
	public String loadInfo(String id) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		FormSet formSet = this.dao.findFormSetByKey(id);

		JSONObject jsonObject = JSONObject.fromObject(formSet);

		String info = "{success:true,data:[" + jsonObject + "]}";

		return info;

	}
	
	public String loadSubInfo(String formId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		FormSet formSet = this.dao.findFormSetByKey(formId);

		JSONObject jsonObject = JSONObject.fromObject(formSet);

		String info = "{success:true,data:[" + jsonObject + "]}";

		return info;

	}

	
	public JSONArray getDataSourceTree(String syrName) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(syrName)) {
			String[] args = { "path" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter syrName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getDataSourceTree(syrName); 
	}

	public String getFieldNames(String svrName,String tableName) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(tableName)) {
			String[] args = { "tableName" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter tableName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(svrName)) {
			String[] args = { "svrName" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter svrName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		StringBuffer result = new StringBuffer();
		List fieldList = new ArrayList();
		fieldList = this.dao.getFields(svrName, tableName);
		// 转化成JSON数组
		JSONArray jsonarr = JSONArray.fromObject(fieldList);
		result.append("{results:")
		.append(jsonarr).append("}");
		return result.toString();
	}
	
	public String getFields(String formId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		FormSet fs = this.dao.findFormSetByKey(formId);
		StringBuffer result = new StringBuffer();
		if(StringUtils.isEmpty(fs.getSvrName())|| StringUtils.isEmpty(fs.getMainTbl())){
			result.append("{results:[]}");
		}else{
			List fieldList = new ArrayList();
			fieldList = this.dao.getFields(fs.getSvrName(), fs.getMainTbl());
			// 转化成JSON数组
			JSONArray jsonarr = JSONArray.fromObject(fieldList);
			result.append("{results:")
			.append(jsonarr).append("}");
		}
		return result.toString();
	}
	
	public String getServersFields(String svrName,String tableName) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(svrName)) {
			String[] args = { "svrName" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter svrName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(tableName)) {
			String[] args = { "tableName" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter tableName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		StringBuffer result = new StringBuffer();

			List fieldList = new ArrayList();
			fieldList = this.dao.getFields(svrName, tableName);
			// 转化成JSON数组
			JSONArray jsonarr = JSONArray.fromObject(fieldList);
			result.append("{results:")
			.append(jsonarr).append("}");
		
		return result.toString();
	}
	
	public String getServerTables(String svrName) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(svrName)) {
			String[] args = { "svrName" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter svrName NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		StringBuffer result = new StringBuffer();
		List tables = this.dao.getServerTables(svrName);
		// 转化成JSON数组
		JSONArray jsonarr = JSONArray.fromObject(tables);
		result.append("{results:")
		.append(jsonarr).append("}");
		return result.toString();
	}
	
	 public String getComboFields(String svrName,String tblName,String itemLabel,String itemValue) 
	    throws Exception{
			// 参数为空
			if (StringUtils.isEmpty(svrName)) {
				String[] args = { "svrName" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter svrName NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			// 参数为空
			if (StringUtils.isEmpty(tblName)) {
				String[] args = { "tblName" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter tblName NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			// 参数为空
			if (StringUtils.isEmpty(itemLabel)) {
				String[] args = { "itemLabel" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter itemLabel NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			// 参数为空
			if (StringUtils.isEmpty(itemValue)) {
				String[] args = { "itemValue" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter itemValue NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			
			FieldItem fieldItem = new FieldItem();
			fieldItem.setItemLabel(itemLabel);
			fieldItem.setItemValue(itemValue);
			fieldItem.setSvrName(svrName);
			fieldItem.setTblName(tblName);
			
			List fields = this.dao.getComboFields(fieldItem);
			
			StringBuffer result = new StringBuffer();
			// 转化成JSON数组
			JSONArray jsonarr = JSONArray.fromObject(fields);
			result.append("{results:")
			.append(jsonarr).append("}");
			return result.toString();
	 }
	 
	 public SysServer getSysServer(String syrName) throws Exception{
			// 参数为空
			if (StringUtils.isEmpty(syrName)) {
				String[] args = { "syrName" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter syrName NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			return this.dao.getSysServer(syrName);
	 }
	 
	 public JSONArray getMainFormSetDataSourceTree(String formId,String paraSeq)
		throws Exception{
			// 参数为空
			if (StringUtils.isEmpty(formId)) {
				String[] args = { "formId" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter formId NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			// 参数为空
			if (StringUtils.isEmpty(paraSeq)) {
				String[] args = { "paraSeq" };
				// 打印错误日志
				StringBuffer msglog = new StringBuffer(
						"empty invoke parameter paraSeq NULL ");
				logger.error(msglog.toString());
				throw new SystemException("error.common.parameter.empty", args);
			}
			return this.dao.getMainFormSetDataSourceTree(formId,paraSeq);
	 }
	
}
